﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessLayer.Tables;
using System.Data.SqlClient;

public partial class Admin_Depo : ChildPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AttributeAdd();
        DropDoldur();
        if (!IsPostBack)
        {
            if (Request.QueryString.Count > 0)
            {
                string id = Request.QueryString["id"];
                KayitBul(id);
            }
        }
    }
    #region Function
    void DropDoldur()
    {
        Db_FiyatTanim ft = new Db_FiyatTanim();
        DataTable dt = ft.GetAllRecord_Datatable("fyt_kod", "fyt_adi ");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem lst = new ListItem();
            lst.Value = dt.Rows[i][0].ToString();
            lst.Text = dt.Rows[i][1].ToString();
            drpDepoSatisFiyat.Items.Add(lst);
            drpDepoSevkFiyat.Items.Add(lst);
            drpsDepoFiyatNo.Items.Add(lst);
        }
    }
    public void KayitBul(string id)
    {
        Db_Depo depo = new Db_Depo();
        depo.DepoKodu = id;
        depo.KayitBul();
        if (depo.DepoKodu != null)
        {
            txtDepoKod.Text = depo.DepoKodu;
            txtDepoAdi.Text = depo.DepoAdi;
            txtAdres1.Text = depo.Adres1;
            txtAdres2.Text = depo.Adres2;
            txtdepogrupkodu.Text = depo.DepoGrupKodu;
            txtDepoKod.Text = depo.DepoKodu;
            drpDepoTip.SelectedItem.Value = depo.DepoTip.ToString();
            txtDepoYetkili.Text = depo.DepoYetkili;
            txtFax.Text = depo.Fax;
            txtIL.Text = depo.Il;
            txtIlce.Text = depo.Ilce;
            txtKasaSayisi.Text = depo.KasaSayisi.ToString();
            txtOtoParkAlani.Text = depo.OtoparkAlani.ToString();
            txtOtoParkKapasite.Text = depo.OtoparkKapasitesi.ToString();
            txtPostaKodu.Text = depo.PostaKod;
            txtRafHacmi.Text = depo.RafHacmi.ToString();
            txtSatisAlani.Text = depo.SatisAlani.ToString();
            txtTel1.Text = depo.Tel1;
            txtTel2.Text = depo.Tel2;
            txtToplamMetrekare.Text = depo.ToplamMetrekare.ToString();
            txtUlke.Text = depo.Ulke;
            if (depo.DepoDefault == true)
                chkDepoDefault.Checked = true;
            else
                chkDepoDefault.Checked = false;
            if (depo.DepoEnvanterHarici == true)
                chkDepoEnvanterHarici.Checked = true;
            else
                chkDepoEnvanterHarici.Checked = false;
        }
    }
    #endregion

    #region ClearArea
    private void CreateArea()
    {
        foreach (System.Web.UI.Control c in pnlControls.Controls)
        {
            if (c is CrmControls.CrmTextBox)
            {
                CrmControls.CrmTextBox t = c as CrmControls.CrmTextBox;
                t.Text = "";
            }
            if (c is CrmControls.CrmCheckBox)
            {
                CrmControls.CrmCheckBox chk = c as CrmControls.CrmCheckBox;
                chk.Checked = false;
            }
            if (c is CrmControls.CrmDropDown)
            {
                CrmControls.CrmDropDown drop = c as CrmControls.CrmDropDown;
                if (drop.SelectedIndex != -1)
                    drop.SelectedIndex = 0;
            }
        }
    }
    #endregion

    #region Kaydet
    private void Kaydet()
    {
        Db_Depo depo = new Db_Depo();
        if (txtDepoKod.Text != "" && txtDepoAdi.Text != "")
        {
            depo.Adres1 = txtAdres1.Text;
            depo.Adres2 = txtAdres2.Text;
            depo.Create_Date = DateTime.Now;
            depo.Create_User = 1;
            depo.DepoAdi = txtDepoAdi.Text;
            if (chkDepoDefault.Checked)
                depo.DepoDefault = true;
            else
                depo.DepoDefault = false;
            if (chkDepoEnvanterHarici.Checked)
                depo.DepoEnvanterHarici = true;
            else
                depo.DepoEnvanterHarici = false;
            //if(drpsDepoFiyatNo.SelectedIndex!=-1)
            // depo.DepoFiyatNo = Int16.Parse(drpsDepoFiyatNo.SelectedItem.Value);
            depo.DepoGrupKodu = txtdepogrupkodu.Text;
            depo.DepoKodu = txtDepoKod.Text;
            if (txtDepoMuhKodu.Text != "")
                depo.DepoMuhKod = int.Parse(txtDepoMuhKodu.Text);
            //if (drpDepoSatisFiyat.SelectedIndex != -1)
            //depo.DepoSatisFiyat =Int16.Parse(drpDepoSatisFiyat.SelectedItem.Value);
            //if (drpDepoSevkFiyat.SelectedIndex != -1)
            //depo.DepoSevkFiyat = Int16.Parse(drpDepoSevkFiyat.SelectedItem.Value);
            if (drpDepoTip.SelectedIndex != -1)
                depo.DepoTip = Int16.Parse(drpDepoTip.SelectedItem.Value);
            depo.DepoYetkili = txtDepoYetkili.Text;
            depo.Fax = txtFax.Text;
            depo.Hidden = true;
            depo.Il = txtIL.Text;
            depo.Ilce = txtIlce.Text;
            if (txtKasaSayisi.Text != "")
                depo.KasaSayisi = Int16.Parse(txtKasaSayisi.Text);
            depo.Locked = true;
            if (txtOtoParkAlani.Text != "")
                depo.OtoparkAlani = decimal.Parse(txtOtoParkAlani.Text);
            if (txtOtoParkKapasite.Text != "")
                depo.OtoparkKapasitesi = decimal.Parse(txtOtoParkKapasite.Text);
            depo.PostaKod = txtPostaKodu.Text;
            if (txtRafHacmi.Text != "")
                depo.RafHacmi = decimal.Parse(txtRafHacmi.Text);
            if (txtSatisAlani.Text != "")
                depo.SatisAlani = decimal.Parse(txtSatisAlani.Text);
            //if (drpSubeID.SelectedIndex != -1)
                //depo.SubeID = int.Parse(drpSubeID.SelectedItem.Value);
                depo.Tel1 = txtTel1.Text;
            depo.Tel2 = txtTel2.Text;
            if (txtToplamMetrekare.Text != "")
                depo.ToplamMetrekare = decimal.Parse(txtToplamMetrekare.Text);
            depo.Ulke = txtUlke.Text;
            depo.Update_Date = DateTime.Now;
            depo.Update_User = 1;
            depo.Save();
            CreateArea();
        }
        else
        {
            if (txtDepoKod.Text == "")
                CrmControls.MessageBx.Show("Depo Kodunu Boş Geçilmez");
            else
                CrmControls.MessageBx.Show("Depo Adı Boş Geçilmez");
        }
    }
    #endregion

    #region AttributeAdd
    private void AttributeAdd()
    {
        imgbtnKaydetKapat.Attributes.Add("onclick", "return Kontrol();");
        imgbtnKaydet.Attributes.Add("onclick", "return Kontrol();");
        imgbtnSil.Attributes.Add("onclick", "return SilKontrol();");
    }
    #endregion
    protected void imgbtnKaydet_Click(object sender, ImageClickEventArgs e)
    {
        Kaydet();
        this.ClientScript.RegisterStartupScript(typeof(Page), "Yenile", "Refresh();", true);
    }
    protected void imgbtnKaydetKapat_Click(object sender, ImageClickEventArgs e)
    {
        Kaydet();
        this.Page.ClientScript.RegisterStartupScript(typeof(Page), "test", "PageClose();", true);
    }
    protected void imgbtnSil_Click(object sender, ImageClickEventArgs e)
    {
        Db_Depo depo = new Db_Depo();
        depo.DepoKodu = txtDepoKod.Text;
        if (depo.DepoKodu != "")
        {
            depo.KayitSil();
            CreateArea();
            this.ClientScript.RegisterStartupScript(typeof(Page), "Tazele", "Refresh();", true);
        }
        else
            CrmControls.MessageBx.Show("Lütfen Silmek İstediğiniz Depo Kodunu Girin");
    }
    protected void imgNewREcord_Click(object sender, ImageClickEventArgs e)
    {
        CreateArea();
    }
}
